package com.myf.warehouse3d.controllers;

import com.myf.ApiResult;
import com.myf.Utility;
import com.myf.dtos.user.UserInput;
import com.myf.dtos.user.UserQueryInput;
import com.myf.dtos.user.UserQueryOutput;
import com.myf.dtos.user.UserUpdatePassword;
import com.myf.features.BaseController;
import com.myf.services.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/api/user")
@Tag(name = "用户功能", description = "用户相关API接口")
public class UserController extends BaseController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @Operation(summary = "新建或保存用户的接口")
    @PutMapping("save")
    public ApiResult save(@RequestBody UserInput userInput) {
        try {
            return ApiResult.createSuccess(userService.save(userInput, getCurrentUser().id));
//            var userEntity = new UserEntity();
//            userEntity.account="test1";
//            userEntity.name="Test1";
//            userEntity.password ="1";
//            userEntity.gender = 0;
//            userEntity.email="test1@warehouse3d.com";
//            userEntity.birthday = null;
//            userEntity.status =1;
//            userEntity.profile = "{}";
//            userEntity.createAt = Utility.getCurrentUTCTime();
//            userEntity.createBy=1;
//            userEntity.version = 0;
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "删除用户接口")
    @DeleteMapping()
    public ApiResult delete(@RequestParam String ids) {
        try {
            var toDeleteIds = new ArrayList<Integer>();
            var temp = ids.split(",");
            for (String id : temp) {
                var res = Utility.tryParse(id);
                if (res != null) {
                    toDeleteIds.add(res);
                }
            }
            if (toDeleteIds.isEmpty()) {
                return ApiResult.createSuccess(0);
            }
            return ApiResult.createSuccess(userService.delete(toDeleteIds, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "查询用户列表接口")
    @PostMapping("query")
    public ApiResult query(@RequestBody UserQueryInput userQueryInput) {
        try {
            return ApiResult.createSuccess(userService.queryUsers(userQueryInput));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "通过用户id获取用户详细信息接口")
    @GetMapping("{id}")
    public ApiResult get(@PathVariable int id) {
        try {
            var user = userService.getById(id);
            var output = new UserQueryOutput();
            output.id = user.id;
            output.account = user.account;
            output.code = user.code;
            output.name = user.name;
            output.status = user.status;
            output.email = user.email;
            output.mobileNo = user.modelNo;
            output.birthday = user.birthday;
            return ApiResult.createSuccess(output);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "通过用户id重置用户密码接口")
    @PutMapping("resetpassword/{id}")
    public ApiResult resetPassword(@PathVariable int id) {
        try {
            return ApiResult.createSuccess(userService.resetPassword(id, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "用户修改密码接口")
    @PutMapping("updatepassword")
    public ApiResult resetPassword(@RequestBody UserUpdatePassword input) {
        try {
            return ApiResult.createSuccess(userService.updatePassword(input, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }
}
